﻿using log4net;
using Mangox.BLL.Stock;
using Mangox.Common;
using Mangox.Common.Tookit;
using Mangox.Model.Common;
using Mangox.Model.MyBusinessDB;
using Mangox.Model.Stock;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web.Http;
using static Mangox.Model.Common.EnumComm;

namespace MGXAPI.Controllers.API
{
    public class UWPDeviceController : ApiController
    {
        static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private static string className = (new StackTrace()).GetFrame(1).GetMethod().ReflectedType.Name + "/";

        /// <summary>
        /// 需要使用分页，还未实现
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<UWP_Device> GetUWP_DeviceList()
        {
            return new UWPDeviceBLL().GetUWP_DeviceList();
        }

        /// <summary>
        /// 根据deviceID获取
        /// http://localhost:13772/api/UWPDevice/GetUWP_Device?deviceID=666
        /// </summary>
        /// <param name="deviceID"></param>
        /// <returns></returns>
        [HttpGet]
        public UWP_Device GetUWP_Device(string deviceID)
        {
            return new UWPDeviceBLL().GetUWP_Device(deviceID);
        }

        /// <summary>
        /// Api，app启动调用；添加用户，同时添加用户活跃
        /// http://localhost:13772/api/UWPDevice/AddUWP_Device
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public void AddUWP_Device(UWPDeviceRequest param)
        {
            if (!MvcApplication.isDebug)
            {
                //添加设备
                var bll = new UWPDeviceBLL();
                var dev = new UWP_Device();
                ObjectReflection.AutoMapping(param, dev);
                var flag = bll.AddUWP_Device(dev);

                //添加用户活跃
                var uae = new UserActive();
                uae.IsFirst = 1;
                uae.DevId = param.DeviceID;
                uae.Source = EnumComm.SourceType.uwp.ToString();
                uae.Versions = param.Versions;
                var flag2 = bll.AddUserActive(uae);

                //添加用户活跃页面，App第一次这里是指首页，客户端减少发起一次db连接
                var uap = new UserActivePage();
                if (param.Source == "uwp")
                {
                    uap.Source = (int)SourceType.uwp;
                }
                else if (param.Source == "applet")
                {
                    uap.Source = (int)SourceType.applet;
                }
                else if (param.Source == "web")
                {
                    uap.Source = (int)SourceType.web;
                }
                uap.DevId = param.DeviceID;
                uap.ActivePage = (int)StockPage.StockInfo;
                var flag3 = bll.AddUserActivePage(uap);
            }
        }

        /// <summary>
        /// Api，appPage启动调用；添加用户使用习惯
        /// http://localhost:13772/api/UWPDevice/AddUserActivePage
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public void AddUserActivePage(ActivePageRequest param)
        {
            if (!MvcApplication.isDebug)
            {
                var bll = new UWPDeviceBLL();
                var model = new UserActivePage();
                ObjectReflection.AutoMapping(param, model);
                var flag = bll.AddUserActivePage(model);
                log.InfoFormat("添加用户使用页面：{0}", JsonCommon.ToJson(param));
            }
        }


    }
}
